namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorCommodityChannelIndex : BaseIndicator
    {
        private LineShape tlsCci;
        private string typField = ("CCI.TYPFIELD." + Common.GetGuid());
        private string typMAField = ("CCI.TYPMAFIELD." + Common.GetGuid());

        public IndicatorCommodityChannelIndex()
        {
            base.IndType = IndicatorType.CCI;
            base.IndName = "CCI";
            base.IndDesc = "顺势指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 14.0));
            base.SourceDic["Close"] = "";
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "CCI" };
        }

        public override bool Contains(object obj)
        {
            return (obj == this.tlsCci);
        }

        public override double[] Get(int r)
        {
            double maValue = 0.0;
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double curValue = ((num2 + num3) + num4) / 3.0;
            maValue = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.typMAField, this.typField, curValue, base.Crab.DataSource);
            double num6 = Common.CalculateAvedev(this.typField, (int) base.IndPeriod[0].ParamValue, curValue, maValue, base.Crab.DataSource, r);
            double num7 = 0.0;
            if (num6 != 0.0)
            {
                num7 = (curValue - maValue) / (0.015 * num6);
            }
            return new double[] { num7, curValue, maValue };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsCci };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsCci);
            base.Crab.DataSource.Columns.Remove(this.TypField);
            base.Crab.DataSource.Columns.Remove(this.TypMAField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsCci.Name, numArray[0], r);
                        base.Crab.SetValue(this.TypField, numArray[1], r);
                        base.Crab.SetValue(this.TypMAField, numArray[2], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsCci = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            base.SourceDic["Close"] = source[0];
            base.SourceDic["High"] = source[1];
            base.SourceDic["Low"] = source[2];
            base.IndPeriod[0].ParamValue = periods[0];
            base.Crab.DataSource.Columns.Add(this.TypField);
            base.Crab.DataSource.Columns.Add(this.TypMAField);
            Common.GetGuid();
            Common.GetGuid();
        }

        public LineShape TlsCci
        {
            get
            {
                return this.tlsCci;
            }
            set
            {
                this.tlsCci = value;
            }
        }

        public string TypField
        {
            get
            {
                return this.typField;
            }
            set
            {
                this.typField = value;
            }
        }

        public string TypMAField
        {
            get
            {
                return this.typMAField;
            }
            set
            {
                this.typMAField = value;
            }
        }
    }
}

